<?php
/**
 * GeryEngine
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this release in the file LICENSE
 *
 * @copyright  Copyright (c) 2007 Arjen Brouwer
 * @license    New BSD License
 * @author     Arjen Brouwer
 */

class HashMap implements Countable, ArrayAccess, IteratorAggregate {

	private $map;
	
	/**
	 * Constructor
	 */
	function __construct($map = null) {
		
		$this->map = (is_array($map)) ? $map : array();
	}
	
	/**
	 * Push an element on top of the hashmap
	 *
	 * @return void
	 */
	function push($element) {
		array_push($this->map, $element);
	}
	
	/**
	 * Pop the last element from this hashmap
	 *
	 * @return mixed
	 */
	function &pop() {
		return array_pop($this->map);
	}
		
	/**
	 * Get the numbers of elements in this hashmap
	 *
	 * @return int
	 */
	function size() {
		return count($this->map);
	}
	
	/**
	 * Get the element on the specified index
	 *
	 * @return mixed
	 */
	function &get($key) {
		if (isset($this->map[$key]))
			return $this->map[$key];
			
		$null = null;
		return $null;
	}
	
	/**
	 * Put a element in the map
	 * 
	 * @param mixed $key
	 * @param mixed $value
	 * @return void
	 */
	function put($key, $value) {
		$this->map[$key] = $value;
	}
	
	function remove($key) {
		unset($this->map[$key]);
	}
	
	/**
	 * Get an array of the keys contained in this map
	 * 
	 * @return array
	 */
	function keys() {
		return array_keys($this->map);
	}
	
	function containsKey($key) {
		return array_key_exists($key, $this->map);
	}
	
	/**
	 * Countable::count()
	 */
	function count() {
	 	return $this->size();
	}
	
	/**
	 * ArrayAccess::offsetExists($offset)
	 *
	 * @return boolean
	 */
	function offsetExists($offset) {
		return $this->containsKey($offset);
	}
	
	/**
	 * ArrayAccess::offsetGet($offset)
	 *
	 * @return mixed
	 */
	function offsetGet($offset) {
		return $this->get($offset);
	}
	
	/**
	 * ArrayAccess::offsetSet($offset, $value) 
	 *
	 * @return void
	 */
	function offsetSet($offset, $value) {
		$this->put($offset, $value);
	}
	
	/**
	 * ArrayAccess::offsetUnset($offset) 
	 *
	 * @return void
	 */
	function offsetUnset($offset) {
		$this->remove($offset);
	}
	
	/**
	 * IteratorAggregate::getIterator()
	 *
	 * @return Iterator
	 */
	function getIterator() {
		return new ArrayIterator($this->map);
	}
}
?>